#!/usr/bin/env python3

import sys
import pygame
import gt
import os

H = 480
W = 600
x = 10
y = 10
x_inc = 10
y_inc = 10
pause = False

def toColor(color):
    if type(color) == tuple:
        return color
    return color >> 16 & 0xFF, color >> 8 & 0xFF, color & 0xFF

def player_init(screen):
    pygame.init()
    gt.run(move, 0.001, [screen])

def player_exit():
    os._exit(0)
    pygame.quit()

def move(screen):
    global x_inc, y_inc, x, y
    if pause:
        return
    else:
        bg = pygame.image.load("./backGround.bmp") # 加载图片
                                #  # 图片路径 [.bmp]原始图片(没有被压缩过的图片)
        player = pygame.image.load("./player.bmp")
        player.set_colorkey((247, 0, 255)) # 去掉背景颜色(抠图)

        screen.blit(bg, (0, 0), (180, 280, 600, 480))


        screen.blit(player, (x, y), (10, 50, 30, 48))

        pygame.display.update()

        if x <= 0 or x >= W - 30:
            x_inc *= -1
        if y <= 0 or y >= H - 48:
            y_inc *= -1

        x += x_inc
        y += y_inc

        gt.run(move, 0.1, [screen])

def player_run(screen):
    global x_inc, y_inc, pause

    while True:
        e = pygame.event.wait()
        if e.type == pygame.QUIT:
            break
        elif e.type == pygame.KEYDOWN:
            if e.key == pygame.K_w:
                y_inc += -1
            elif e.key == pygame.K_s:
                y_inc += 1
            elif e.key == pygame.K_a:
                x_inc += -1
            elif e.key == pygame.K_d:
                x_inc += 1
            elif e.key == pygame.K_ESCAPE or e.key == pygame.K_q:
                break
            elif e.key == pygame.K_SPACE:
                if pause:
                    gt.run(move, 0.1, [screen])

                pause = not pause


if __name__ == '__main__':
    screen = pygame.display.set_mode((W, H))

    player_init(screen)

    player_run(screen)

    player_exit()
